unit MainForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, SchemaEditorForm,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.ExtCtrls, Vcl.StdCtrls,
  Project, OmniXML;

type

  TForm1 = class(TForm)
    Panel1: TPanel;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    loadProjectMenutItem: TMenuItem;
    exitMenutItem: TMenuItem;
    N1: TMenuItem;
    Panel4: TPanel;
    About1: TMenuItem;
    Schema1: TMenuItem;
    About2: TMenuItem;
    Label2: TLabel;
    tablesListBox: TListBox;
    PopupMenu1: TPopupMenu;
    generateMenuItem: TMenuItem;
    selectAllMenuItem: TMenuItem;
    deselectAllMenuItem: TMenuItem;
    N2: TMenuItem;
    selectAllCheckBox: TCheckBox;
    buttonGenerate: TButton;
    procedure generateMenuItemClick(Sender: TObject);
    procedure loadProjectMenutItemClick(Sender: TObject);
    procedure exitMenutItemClick(Sender: TObject);
    procedure selectAllCheckBoxClick(Sender: TObject);
    procedure selectAllMenuItemClick(Sender: TObject);
    procedure deselectAllMenuItemClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    project: TProject;
    function getProject(): TProject;
    procedure loadSchemaFile();
    procedure setSchemaLoaded(loaded: boolean);
    procedure selectAllTables(select: boolean);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  ORMGenerator, FileCtrl;

{$R *.dfm}

procedure TForm1.deselectAllMenuItemClick(Sender: TObject);
begin
  selectAllCheckBox.Checked := false;
  selectAllCheckBoxClick(self);
end;

procedure TForm1.exitMenutItemClick(Sender: TObject);
begin
  Form1.Close;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  Left := (Screen.Width - Width) div 2;
  Top := (Screen.Height - Height) div 2;
end;

procedure TForm1.generateMenuItemClick(Sender: TObject);
const
  SELDIRHELP = 1000;
var
  orm: TORM;
  I: Integer;
  tables: TStringArray;
  dir: String;
  j: integer;
begin
  if (tablesListBox.SelCount > 0) then
  begin
    dir := GetCurrentDir;
    if (SelectDirectory(dir, [sdAllowCreate, sdPerformCreate, sdPrompt], SELDIRHELP)) then
    begin
      for I := 0 to tablesListBox.GetCount - 1 do
      begin
        if (tablesListBox.Selected[i]) then
          begin
          j := Length(tables);
          SetLength(tables, Length(tables) + 1);
          tables[j] := tablesListBox.Items.Strings[i];
          end;
      end;
      if (Length(tables) <> 0) then
        begin
        orm := TORM.Create(getProject().getSchemaPath(), tables);
        orm.generate(dir);
        ShowMessage('Classes succesfully created in ' + dir);
        end;
    end;
  end else
    ShowMessage('No tables selected');
end;

function TForm1.getProject: TProject;
begin
  if (project = nil) then
    project := TProject.Create();
  Result := project;
end;

procedure TForm1.loadProjectMenutItemClick(Sender: TObject);
var
  openDialog: TOpenDialog;
begin
  openDialog := TOpenDialog.Create(self);
  openDialog.Title := 'Load schema';
  openDialog.InitialDir := GetCurrentDir;
  openDialog.Filter := 'Schema XML file|*.xml';
  openDialog.DefaultExt := 'xml';
  openDialog.FilterIndex := 1;
  if openDialog.Execute then
    begin
    setSchemaLoaded(false);
    if getProject().load(openDialog.FileName)
      then begin
        loadSchemaFile();
        setSchemaLoaded(true);
        ShowMessage('Schema succesfully loaded');
      end
      else begin
        ShowMessage('Problem occured loading schema');
      end;
    end;
  openDialog.Free;
end;

procedure TForm1.loadSchemaFile;
var
  tables: IXMLNodeList;
  table: IXMLNode;
  iTable: integer;
begin
  tablesListBox.Items.BeginUpdate;
  tablesListBox.Items.Clear;
  tables := getProject().getSchemaFile().DocumentElement.SelectNodes('table');
  for iTable := 0 to tables.length - 1 do
  Begin
    table := tables.Item[iTable];
    if (table.SelectSingleNode('name') <> nil) then
      tablesListBox.Items.add(Trim(table.SelectSingleNode('name').Text));
  End;
  setSchemaLoaded(true);
  tablesListBox.Items.EndUpdate;
  tablesListBox.Repaint;
end;

procedure TForm1.selectAllCheckBoxClick(Sender: TObject);
begin
  selectAllTables(selectAllCheckBox.Checked);
end;

procedure TForm1.selectAllMenuItemClick(Sender: TObject);
begin
  selectAllCheckBox.Checked := true;
  selectAllCheckBoxClick(self);
end;

procedure TForm1.selectAllTables(select: boolean);
begin
  if (select)
    then tablesListBox.SelectAll
    else begin
      tablesListBox.Items.BeginUpdate;
      tablesListBox.MultiSelect := False;
      tablesListBox.ItemIndex := -1;
      tablesListBox.MultiSelect := True;
      tablesListBox.Items.EndUpdate;
    end;
end;

procedure TForm1.setSchemaLoaded(loaded: boolean);
begin
  generateMenuItem.Enabled := loaded;
  buttonGenerate.Enabled := loaded;
  selectAllCheckBox.Enabled := loaded;
  selectAllMenuItem.Enabled := loaded;
  deselectAllMenuItem.Enabled := loaded;
end;

end.
